<?php

namespace App\Http\Controllers\Salary;



use App\Models\System\SysLog;
use App\Models\User;
use App\Models\Salary;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use App\Http\Requests\Salary\SalaryShowRequest;
use App\Http\Requests\Salary\SalaryIndexRequest;
use App\Http\Requests\Salary\SalaryStoreRequest;
use App\Http\Requests\Salary\SalaryUpdateRequest;
use App\Http\Requests\Salary\SalaryDestroyRequest;

class SalaryController extends Controller
{
    /**
     * 工资方案列表
     *
     * Date: 2018/5/22
     * @author Sam
     * @param SalaryIndexRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(SalaryIndexRequest $request)
    {
        $condition = array_filter($this->extractInputFromRules($request,$request->rules()));
        $query = DB::table('salaries as s');
        $query->select(['s.*','po.name as position_name','pr.name as project_name']);
        $query->leftJoin('users as u','s.user_id','=','u.id');
        $query->leftJoin('positions as po','u.position_id','=','po.id');
        $query->leftJoin('projects as pr','u.project_id','=','pr.id');
        if(!empty($condition['user_name'])){
            $query->where('s.user_name','like',"%{$condition['user_name']}%");
            unset($condition['user_name']);
        }
        if(!empty($condition['bank_card'])){
            $query->where('bank_card','like',"%{$condition['bank_card']}%");
            unset($condition['bank_card']);
        }
        if(!empty($condition['position_id'])){
            $query->where('po.id',$condition['position_id']);
            unset($condition['position_id']);
        }
        if(!empty($condition['project_id'])){
            $query->where('pr.id',$condition['project_id']);
            unset($condition['project_id']);
        }
        $list = $query->where($condition)->get();
        return success($list);
    }

    /**
     * 创建工资方案
     *
     * Date: 2018/5/22
     * @author Sam
     * @param SalaryStoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(SalaryStoreRequest $request)
    {
        $attr = $this->extractInputFromRules($request,$request->rules());
        $user = User::find($attr['user_id']);
        $attr['user_name'] = $user->name;
        $salary = Salary::query()->create($attr);
        SysLog::log(Auth::user(),'薪资方案模块','创建薪资方案');
        return success($salary);
    }

    /**
     * 获取单条工资方案
     *
     * Date: 2018/5/22
     * @author Sam
     * @param SalaryShowRequest $request
     * @param Salary $salary
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(SalaryShowRequest $request,$salary)
    {
        $res = DB::table('salaries as s')
            ->select(['s.*','po.name as position_name','pr.name as project_name'])
            ->leftJoin('users as u','s.user_id','=','u.id')
            ->leftJoin('positions as po','u.position_id','=','po.id')
            ->leftJoin('projects as pr','u.project_id','=','pr.id')
            ->where('s.id',$salary)
            ->first();
        return success($res);
    }


    /**
     * 修改工资方案
     *
     * Date: 2018/5/22
     * @author Sam
     * @param SalaryUpdateRequest $request
     * @param Salary $salary
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     */
    public function update(SalaryUpdateRequest $request, Salary $salary)
    {
        $attr = $this->extractInputFromRules($request,$request->rules());
        $user = User::find($attr['user_id']);
        $attr['user_name'] = $user->name;
        foreach ($attr as $k => $v){
            $salary->$k = $v;
        }
        $salary->saveOrFail();
        SysLog::log(Auth::user(),'薪资方案模块','更新薪资方案');
        return updated($salary);
    }

    /**
     * 删除工资方案
     *
     * Date: 2018/5/22
     * @author Sam
     * @param SalaryDestroyRequest $request
     * @param Salary $salary
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destroy(SalaryDestroyRequest $request, Salary $salary)
    {
        $salary->delete();
        SysLog::log(Auth::user(),'薪资方案模块','删除薪资方案');
        return deleted();
    }
}
